$(document).ready(function () {
    fetchNotifications();

    $("#remove-btn").click(function () {
        var ids = $.map($('#notifications-list').bootstrapTable('getSelections'), function (row) {
            return row.id;
        });

        for (var i = 0; i < ids.length; i++) {
            //ids[i]
            $.ajax({
                type: "GET",
                data: { MessageID: ids[i]},
                url: "../Handle/SysMessageHandlerService.ashx?method=Del_One_Family_Message_From_ID",
                dataType: "json",
                success: function (data) {
                    if(1 == data.MessageType) {
                        // alert("消息删除成功！");
                        $("#dialog-modal-label").text("消息删除成功！");
                        $("#DialogModal").modal("show");
                        fetchNotifications();
                    } else {
                        // alert("消息删除失败！");
                        $("#dialog-modal-label").text("消息删除失败！");
                        $("#DialogModal").modal("show");
                        return;
                    }
                }
            });
        }
        $('#notifications-list').bootstrapTable('remove', {
            field: 'id',
            values: ids
        });
    });
});

function fetchNotifications() {
    $('#notifications-list').bootstrapTable({
        method: 'get', 
        dataType: "json", 
        pageSize: 10,  
        pageNumber: 1, 
        toolbar: "#toolbar",
        sortName: "XID",
        sortOrder: "asc",
        clickToSelect: "true",
        sidePagination: "server",
        queryParamsType: "",
        queryParams: getParams,  
        pagination: true,
        pageList: "[20, All]",
        search: "true",
        url: "../Handle/SysMessageHandlerService.ashx?method=Get_One_Family_Message_From_UID",
        responseHandler: function (res) {
            res.total = res.RecordCount;
            //console.log(res);
            return res;
        },
        onClickRow: function (row) {
            //console.log(row);
            $("#msg-title").text(row.title);
            $("#msg-content").text(row.msg);
            $("#msg-time").text(row.CurrentDate);
            $("#NotificationDetailModal").modal("show");
            $.ajax({
                type: "GET",
                data: { MessageID: row.id},
                url: "../Handle/SysMessageHandlerService.ashx?method=Mark_One_SysMsg_Readed",
                dataType: "json",
                success: function (data) {
                    if(1 != data.MessageType) {
                        alert("标记为已读报错，请联系管理员报告问题");
                    } else {
                        $("#notifications-list").bootstrapTable('refresh', {});
                    }
                }
            });
        }, 
        
    });
}

function getParams(params) {
    var temp = { 
        PageSize : params.pageSize, 
        PageIndex : params.pageNumber
    };
    return temp;
}

function cellStyle(value, row, index) {
    if (0 == row.flag) {
        return {
            classes: "text-bold"
        };
    }
    return {};
}